/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.app.ad.service.impl;

import me.zhengjie.modules.app.ad.domain.AduserXMovie;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.app.ad.repository.AduserXMovieRepository;
import me.zhengjie.modules.app.ad.service.AduserXMovieService;
import me.zhengjie.modules.app.ad.service.dto.AduserXMovieDto;
import me.zhengjie.modules.app.ad.service.dto.AduserXMovieQueryCriteria;
import me.zhengjie.modules.app.ad.service.mapstruct.AduserXMovieMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.hutool.core.util.IdUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author HaojieLi
* @date 2021-03-29
**/
@Service
@RequiredArgsConstructor
public class AduserXMovieServiceImpl implements AduserXMovieService {

    private final AduserXMovieRepository aduserXMovieRepository;
    private final AduserXMovieMapper aduserXMovieMapper;

    @Override
    public Map<String,Object> queryAll(AduserXMovieQueryCriteria criteria, Pageable pageable){
        Page<AduserXMovie> page = aduserXMovieRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(aduserXMovieMapper::toDto));
    }

    @Override
    public List<AduserXMovieDto> queryAll(AduserXMovieQueryCriteria criteria){
        return aduserXMovieMapper.toDto(aduserXMovieRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public AduserXMovieDto findById(String id) {
        AduserXMovie aduserXMovie = aduserXMovieRepository.findById(id).orElseGet(AduserXMovie::new);
        ValidationUtil.isNull(aduserXMovie.getId(),"AduserXMovie","id",id);
        return aduserXMovieMapper.toDto(aduserXMovie);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AduserXMovieDto create(AduserXMovie resources) {
        resources.setId(IdUtil.simpleUUID()); 
        return aduserXMovieMapper.toDto(aduserXMovieRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(AduserXMovie resources) {
        AduserXMovie aduserXMovie = aduserXMovieRepository.findById(resources.getId()).orElseGet(AduserXMovie::new);
        ValidationUtil.isNull( aduserXMovie.getId(),"AduserXMovie","id",resources.getId());
        aduserXMovie.copy(resources);
        aduserXMovieRepository.save(aduserXMovie);
    }

    @Override
    public void deleteAll(String[] ids) {
        for (String id : ids) {
            aduserXMovieRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<AduserXMovieDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (AduserXMovieDto aduserXMovie : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("广告商id", aduserXMovie.getAdUserId());
            map.put("视频地址", aduserXMovie.getMovieUrl());
            map.put("创建者", aduserXMovie.getCreateBy());
            map.put("更新者", aduserXMovie.getUpdateBy());
            map.put("创建时间", aduserXMovie.getCreateTime());
            map.put("更新时间", aduserXMovie.getUpdateTime());
            map.put("数据状态", aduserXMovie.getIsEnable());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}